package webbandidong.util;


import java.util.Hashtable;

public class PagingHelper {

    private static final int LINK_NUMBER_PER_PAGE = Constant.LINK_NUMBER_PER_PAGE;

    public static String pageLinks(PageInfo pageInfo, String url, String cssClassName) {
        int totalPages = (int)(Math.ceil((double)pageInfo.totalItems / pageInfo.pageSize));
        int ceilSections = (int)(Math.ceil((double)totalPages / LINK_NUMBER_PER_PAGE));
        int floorSection = totalPages / LINK_NUMBER_PER_PAGE;
        int remainderSection = totalPages % LINK_NUMBER_PER_PAGE;
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));

        String linkStr = null;
        if (tempSectionNum == 1) {
            if (floorSection == 0 || (floorSection == 1 && remainderSection == 0)) {
                linkStr = renderPageNumberLinks(pageInfo, url, cssClassName);
            }
            else {
                linkStr = renderPageNumberLinks(pageInfo, url, cssClassName) +
                        renderNextLink(pageInfo, url);
            }
        }
        else if ((1 < tempSectionNum && tempSectionNum < ceilSections && remainderSection != 0) ||
            (1 < tempSectionNum && tempSectionNum < floorSection)) {
            
            linkStr = renderPreviousLink(pageInfo, url) + 
                    renderPageNumberLinks(pageInfo, url, cssClassName) +
                    renderNextLink(pageInfo, url);
        }
        else {
            linkStr = renderPreviousLink(pageInfo, url) +
                    renderPageNumberLinks(pageInfo, url, cssClassName);
        }

        return linkStr;
    }

    public static String pageLinks(PageInfo pageInfo, String url, String cssClassName,
            Hashtable<String, String> table, Hashtable<String, String> func) {
        int totalPages = (int)(Math.ceil((double)pageInfo.totalItems / pageInfo.pageSize));
        int ceilSections = (int)(Math.ceil((double)totalPages / LINK_NUMBER_PER_PAGE));
        int floorSection = totalPages / LINK_NUMBER_PER_PAGE;
        int remainderSection = totalPages % LINK_NUMBER_PER_PAGE;
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));

        String linkStr = null;
        if (tempSectionNum == 1) {
            if (floorSection == 0 || (floorSection == 1 && remainderSection == 0)) {
                linkStr = renderPageNumberLinks(pageInfo, url, cssClassName, table, func);
            }
            else {
                linkStr = renderPageNumberLinks(pageInfo, url, cssClassName, table, func) +
                        renderNextLink(pageInfo, url, table, func);
            }
        }
        else if ((1 < tempSectionNum && tempSectionNum < ceilSections && remainderSection != 0) ||
            (1 < tempSectionNum && tempSectionNum < floorSection)) {

            linkStr = renderPreviousLink(pageInfo, url, table, func) + 
                    renderPageNumberLinks(pageInfo, url, cssClassName, table, func) +
                    renderNextLink(pageInfo, url, table, func);
        }
        else {
            linkStr = renderPreviousLink(pageInfo, url, table, func) +
                    renderPageNumberLinks(pageInfo, url, cssClassName, table, func);
        }

        return linkStr;
    }

    private static String renderPageNumberLinks(PageInfo pageInfo, String url, String cssClassName, 
            Hashtable<String, String> table, Hashtable<String, String> func) {
        int totalPages = (int)(Math.ceil((double)pageInfo.totalItems / pageInfo.pageSize));
        int ceilSections = (int)(Math.ceil((double)totalPages / LINK_NUMBER_PER_PAGE));
        int floorSection = totalPages / LINK_NUMBER_PER_PAGE;
        int remainderSection = totalPages % LINK_NUMBER_PER_PAGE;
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));

        int startPageOfTempSection = (tempSectionNum - 1) * LINK_NUMBER_PER_PAGE + 1;
        int finishPageOfTempSection;

        if (tempSectionNum <= floorSection) {
            finishPageOfTempSection = startPageOfTempSection + LINK_NUMBER_PER_PAGE - 1;
        }
        else {
            finishPageOfTempSection = startPageOfTempSection + remainderSection - 1;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = startPageOfTempSection; i <= finishPageOfTempSection; i++) {
            String str = "<a ";
            str += "href='" + url + "?";
            if (table != null) {
                for (String s : table.keySet()) {
                    str += s + "=" + table.get(s) + "&";
                }
            }
            str += "page=" + i +"' ";
            if (i == pageInfo.currentPage) {
                str += "class='" + cssClassName + "' ";
            }
            if (func != null) {
                for (String f : func.keySet()) {
                    str += f + "='" + func.get(f) + "' ";
                }
            }
            str += ">";
            str += i + "</a>";

            builder.append(str);
            builder.append("&nbsp;");
        }

        return builder.toString();
    }

    private static String renderPageNumberLinks(PageInfo pageInfo, String url, String cssClassName) {
        int totalPages = (int)(Math.ceil((double)pageInfo.totalItems / pageInfo.pageSize));
        int ceilSections = (int)(Math.ceil((double)totalPages / LINK_NUMBER_PER_PAGE));
        int floorSection = totalPages / LINK_NUMBER_PER_PAGE;
        int remainderSection = totalPages % LINK_NUMBER_PER_PAGE;
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));

        int startPageOfTempSection = (tempSectionNum - 1) * LINK_NUMBER_PER_PAGE + 1;
        int finishPageOfTempSection;

        if (tempSectionNum <= floorSection) {
            finishPageOfTempSection = startPageOfTempSection + LINK_NUMBER_PER_PAGE - 1;
        }
        else {
            finishPageOfTempSection = startPageOfTempSection + remainderSection - 1;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = startPageOfTempSection; i <= finishPageOfTempSection; i++) {
            String str = "<a ";
            str += "href='" + url + "?page=" + i +"' ";
            if (i == pageInfo.currentPage) {
                str += "class='" + cssClassName + "'";
            }
            str += ">";
            str += i + "</a>";
       
            builder.append(str);
            builder.append("&nbsp;");
        }

        return builder.toString();
    }

    private static String renderNextLink(PageInfo pageInfo, String url, 
            Hashtable<String, String> table, Hashtable<String, String> func) {
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));
        int nextPage = tempSectionNum * LINK_NUMBER_PER_PAGE + 1;

        String tag = "<a href='";
        tag += url + "?";
        if (table != null) {
            for (String s : table.keySet()) {
                tag += s + "=" + table.get(s) + "&";
            }
        }
        tag += "page=" + nextPage + "' ";
        if (func != null) {
            for (String fun : func.keySet()) {
                tag += fun + "='" + func.get(fun) + "' ";
            }
        }
        tag += ">next" + "</a>";

        return "&nbsp;&nbsp;&nbsp;" + tag;
    }

    private static String renderNextLink(PageInfo pageInfo, String url) {
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));
        int nextPage = tempSectionNum * LINK_NUMBER_PER_PAGE + 1;

        String tag = "<a href='";
        tag += url + "?page=" + nextPage + "'>";
        tag += "next" + "</a>";

        return "&nbsp;&nbsp;&nbsp;" + tag;
    }

    private static String renderPreviousLink(PageInfo pageInfo, String url) {
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));
        int previousPage = (tempSectionNum - 1) * LINK_NUMBER_PER_PAGE;

        String tag = "<a href='";
        tag += url + "?page=" + previousPage + "'>";
        tag += "prev" + "</a>";

        return tag + "&nbsp;&nbsp;&nbsp;";
    }

    private static String renderPreviousLink(PageInfo pageInfo, String url, 
            Hashtable<String, String> table, Hashtable<String, String> func) {
        int tempSectionNum = (int)(Math.ceil((double)pageInfo.currentPage / LINK_NUMBER_PER_PAGE));
        int previousPage = (tempSectionNum - 1) * LINK_NUMBER_PER_PAGE;

        String tag = "<a href='";
        tag += url + "?";
        if (table != null) {
            for (String s : table.keySet()) {
                tag += s + "=" + table.get(s) + "&";
            }
        }
        tag += "page=" + previousPage + "' ";
        if (func != null) {
            for (String fun : func.keySet()) {
                tag += fun + "='" + func.get(fun) + "' ";
            }
        }
        tag += ">prev" + "</a>";

        return tag + "&nbsp;&nbsp;&nbsp;";
    }
}
